<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CateController extends Controller
{

    /**
     * 处理前台数据
     */
    static public function getCatePid($cpid)
    {
        $data = DB::table('cate')->where('cpid',$cpid)->get();
        $arr = [];
        foreach($data as $key=>$value)
        {
            $value['sub'] = self::getCatePid($value['cid']);
            $arr[] = $value;
        }
        return $arr;

    }

    /**
     * 添加字段
     */
    static public function getCates(){
        $data = DB::select(" select * ,concat(cpath,',',cid)as paths from m9_cate order by  paths");
        foreach($data as $key => $v){
            $len = substr_count($v['cpath'],',');
           //重复使用一个字符串
           $data[$key]['cname'] = str_repeat('|----',$len).$v['cname'];
        }
        return $data;
    }
    /**
     * 分类主页面
     */
    public function getIndex()
    {
        //dd(self::getCatePid(0));

        $s = self::getCates();
       
        return view('admin.cate.index',['data'=>$s]);
        //echo "cate的主页";
    }


    /**
     * 分类添加
     */
    public function getAdd($cid = '')
    {

        //获取所有分类
        $s = self::getCates();
        return view('admin.cate.add',['data'=>$s,'cid'=>$cid]);
    }
    /**
     * 分类添加处理
     */
    public function postInsert(Request $request)
    {
        $temp = $request -> except('_token');
       
        $pid = $temp['cpid'];
        //dd($pid);

        //拼接cpath字段
        if($pid == 0){
            $temp['cpath'] = 0;
        }else{
            $parentData = DB::table('cate')->where('cid',$pid)->first();
            $temp['cpath'] = $parentData['cpath'].','.$parentData['cid'];
        }
        //将数据插入到数据库
        $res = DB::table('cate')->insert($temp);
        if($res){
            return redirect('/admin/cate/index')->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }

    /**
     * 删除
     */
    public function getDelete($cid){
        //
        $res = DB::table('cate')->where('cpid',$cid)->first();
        if($res){
            return back()->with('error','当前类别有子类，不能删');
        }


        $res = DB::table('cate')->where('cid',$cid)->delete();
        if($res){
            return redirect('/admin/cate/index')->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    /**
     * 修改
     */
    public function getEdit( Request $request , $cid){
        $res = DB::table('cate')->where('cid',$cid)->first();
         $s = self::getCates();
        return view('/admin/cate/edit',['data'=>$s,'res'=>$res]);
    }

    public function postUpdata(Request $request , $cid){
        $res = DB::table('cate')->where('cpid',$cid)->first();
        if($res){
            return back()->with('error','当前类别有子类，不能修改');
        }

        $temp = $request -> except('_token');
       
        $pid = $temp['cpid'];
        //dd($pid);

        //拼接cpath字段
        if($pid == 0){
            $temp['cpath'] = 0;
        }else{
            $parentData = DB::table('cate')->where('cid',$pid)->first();
            $temp['cpath'] = $parentData['cpath'].','.$parentData['cid'];
        }
        //修改数据库
        $res = DB::table('cate')->where('cid',$cid)->update($temp);
        if($res){
            return redirect('/admin/cate/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }



}
